<!DOCTYPE html>
<html lang="en" style="height: 100%">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/vue.js"></script>
    <script src="js/jquery-3.3.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
</head>
<style>
    img{
        width: 242px;
        height: 200px;
        margin-right: 10px;
        object-fit: cover;
    }
</style>
<body style="height: 100%;">
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html">首页</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="#" style="color: gray">后台管理系统</a></li>
                <li><a href="#">当前类型:</a></li>
                <li><a href="#" id="typename">用户</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">搜索类型 <span class="caret"></span></a>
                    <ul class="dropdown-menu" id="types">
                        <li><a href="#" onclick="setSearchType('用户')">用户</a></li>
                        <li><a href="#" onclick="setSearchType('封禁用户')">封禁用户</a></li>
                        <li><a href="#" onclick="setSearchType('图片')">图片</a></li>
                        <li><a href="#" onclick="setSearchType('待审核图片')">待审核图片</a></li>
                        <li><a href="#" onclick="setSearchType('评论')">评论</a></li>
                    </ul>
                </li>
            </ul>
            <div action="#" class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search" id="search">
                </div>
                <button class="btn btn-default" onclick="mySearch(1)">搜索</button>
            </div>
            <ul class="nav navbar-nav navbar-right">
                <li id="userpic"></li>
                <li style="border-right:1px solid silver;"><a href="#">用户名:</a></li>
                <li ><a href="mine.html" id="name1">请登录</a></li>
                <li id="backBtn">
                    <a href="index.html">返回前台</a>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

<DIV class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas" style="border-right:1px solid silver;height: 100%" id="sidebar">
    <UL class="nav nav-sidebar">

        <LI><A class="accordion-toggle collapsed" style="color: gray;border-bottom: 1px solid silver" href="#" data-toggle="collapse" data-target="#mastersubmenu">用户维护</A>
            <UL class="nav collapse" id="mastersubmenu" style="padding-left: 20px;">
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getUsers()">用户列表</A></LI>
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getBanUser()">封禁用户列表</A></LI>
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getDeposit()">充值列表</A></LI>
            </UL>
        </LI>
        <LI><A class="accordion-toggle collapsed" style="color: gray;border-bottom: 1px solid silver" href="#" data-toggle="collapse" data-target="#mastersubmenu2">图片维护</A>
            <UL class="nav collapse" id="mastersubmenu2" style="padding-left: 20px;">
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getImgs()">图片列表</A></LI>
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getComments()">评论列表</A></LI>
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getComplains()">举报评论列表</A></LI>
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getAuditImgs()">待审核图片列表</A></LI>
            </UL>
        </LI>
        <LI><A class="accordion-toggle collapsed" style="color: gray;border-bottom: 1px solid silver" href="#" data-toggle="collapse" data-target="#mastersubmenu3">图片类型维护</A>
            <UL class="nav collapse" id="mastersubmenu3" style="padding-left: 20px;">
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="getTypes()">类型列表</A></LI>
                <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="addTypeView()">添加新类型</A></LI>
            </UL>
        </LI>
        <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="updateAdmin()">修改账号信息</A></LI>
        <LI><A style="color: gray;border-bottom: 1px solid silver" href="#" onclick="logout()">注销</A></LI>

        <!--<LI><A style="color: gray" href="changePassword.php">密码维护</A></LI>-->

    </UL>
</DIV>
<DIV id="bigDiv" class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main " style="position:absolute;">
    <table id="users" class="table table-hover">

    </table>
    <hr>
    <div style="text-align: center;" id="page">
        <nav aria-label="Page navigation">
            <ul class="pagination pagination-lg" id="skips">
<!--                <li>-->
<!--                    <a href="#" aria-label="Previous" onclick="" id="skippre">-->
<!--                        <span aria-hidden="true">&laquo;</span>-->
<!--                    </a>-->
<!--                </li>-->
<!--                <li><a id="skip1" href="#" onclick="">1</a></li>-->
<!--                <li><a id="skip2" href="#" onclick="">2</a></li>-->
<!--                <li><a id="skip3" href="#" onclick="">3</a></li>-->
<!--                <li><a id="skip4" href="#" onclick="">4</a></li>-->
<!--                <li><a id="skip5" href="#" onclick="">5</a></li>-->
<!--                <li>-->
<!--                    <a id="skipnex" href="#" aria-label="Next" onclick="">-->
<!--                        <span aria-hidden="true">&raquo;</span>-->
<!--                    </a>-->
<!--                </li>-->
            </ul>
        </nav>
        <style>
            .c{
                display:inline;
            }
        </style>
        <div style="text-align: center">
            <input  class="form-control c" type="number"style="width:60px;" id="toPageNum">
            <button class="btn btn-default" id="toPageBtn" onclick="skipToPage()">跳转</button>
        </div>
        <h4>当前页数<span class="label label-default" id="curpage"></span></h4>
    </div>
    <hr>
    <div style="text-align: center;"><a href="#" onclick="gotoAdmin()">Admin</a></div>
    <div style="text-align: center;"><span>官方邮箱: wsq981027@163.com</span></div>
</DIV>
</body>
<script src="js/checkToken.js"></script>
<script>
    $('#page').hide();
    var searchType='用户';
    var type='search';
    if (sessionStorage.getItem('page2')==null)
        sessionStorage.setItem('page2',1);
    var name=sessionStorage.getItem('adminName');
    var adminPic=sessionStorage.getItem('adminPic');
    var adminid=sessionStorage.getItem('adminid');
    document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
    if(name!='null') {
        document.getElementById('userpic').innerHTML = '<img class="img-circle" src="/' + adminPic + '" alt="" style="width: 50px;height: 50px">';
        document.getElementById('name1').innerText ='管理员:'+name;
    }else {
        alert('请登录管理员账号');
        window.location.href='index.html';
    }

    var pages=0;

    var curpage=sessionStorage.getItem('page2');
    console.log('当前页数：'+curpage);
    curpage=curpage==null?1:curpage;
    $('#toPageNum').val(curpage);

    function skipToPage() {
        var toPageNum=$('#toPageNum').val();
        skip(type,toPageNum);
    }

    $("#search").bind("keypress", function(){
        if (event.keyCode == 13){
            // 触发需要调用的方法
            mySearch(1);
        }
    })

    function updateAdmin() {
        $('#users').empty();
        $('#page').hide();
        $(document).keypress(function(e) {
            if (e.keyCode == 13) {
                // 触发需要调用的方法
                update();
            }
        })
        $('#users').append('<div class="form-horizontal"> <div class="form-group"> <label for="pic" class="col-sm-2 control-label">头像</label> <div class="col-sm-10"> <input type="file" style="width: 25%;" class="form-control" id="pic" name="pic"> </div> </div> <div class="form-group"> <label for="pic" class="col-sm-2 control-label">用户名</label> <div class="col-sm-10"> <input type="text" style="width: 25%;" class="form-control" id="user" name="user"> </div> </div> <div class="form-group" style="text-align: left"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" onclick="update()">修改</button> </div> </div> </div>');
    }

    function addTypeView() {
        $('#users').empty();
        $('#page').hide();
        $('#users').append('<div class="form-horizontal">  <div class="form-group"> <label for="pic" class="col-sm-2 control-label">类型名</label> <div class="col-sm-10"> <input type="text" style="width: 25%;" class="form-control" id="type" name="type"> </div> </div> <div class="form-group" style="text-align: left"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" onclick="addType()">添加</button> </div> </div> </div>');
    }

    function addType() {
        var type=document.getElementById('type').value;
        $.ajax({
            url:'/admin/addType.do?type='+type,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            type:'get',
            success:function (datas) {
                checkToken(datas);
                if (datas.resMsg=='添加成功')
                    alert('添加成功');
                else
                    alert(datas.resMsg);
            }
        })
    }

    function setSearchType(searchtype) {
        searchType=searchtype;
        document.getElementById('typename').innerText=searchType;
    }
    function mySearch(page) {

        sessionStorage.setItem('page2',page);
        type='search';
        search=document.getElementById('search').value;
        if (search==''){
            alert('请输入搜索信息');
            return;
        }
            $.ajax({
                url: "/hui/TestController/search.do?name=" + search+"&type="+searchType,
                type: "get",
                success: function (datas) {
                    skipPages(datas.result.length);

                    console.log("search" + datas.result);
                        $('#users').empty();
                        $('#page').show();
                    $('#users').append('<tbody>');
                    if (searchType=='图片') {
                            $('#users').append('<tr> <td>编号</td> <td>类型</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>上传人</td> <td>点赞数</td> <td>操作</td> </tr>');
                            for (var i=(page-1)*10;i<page*10;i++) {
                                var n = parseInt(i) + 1;
                                $.ajax({
                                    url: "/hui/TestController/getUserById.do?userid=" + datas.result[i]['userid'],
                                    type: "get",
                                    async: false,
                                    success: function (data) {
                                        $.ajax({
                                            url: "/admin/getTypeById.do?typeid=" + datas.result[i]['typeid'],
                                            type: "get",
                                            async: false,
                                            headers:{
                                                accessToken:localStorage.getItem('adminAccessToken')
                                            },
                                            success: function (datat) {
                                                checkToken(datat);
                                                $('#users').append('<tr><td>' + n + '</td><td>' + datat.result.name + '</td><td>' + datas.result[i]['title'] + '</td><td>' + datas.result[i]['intro'] + '</td><td><img class="img img-rounded" src="/' + datas.result[i]['imgname'] + '" alt="" style="width: 50px;height: 50px"></td><td>' + data.result['name'] + '</td><td>' + datas.result[i]['thumb'] + '</td><td><a href="#" onclick="deleteImgById(' + datas.result[i]['id'] + ')">删除</a></td></tr>');
                                            }
                                        })
                                    }
                                })
                            }
                        }
                        if (searchType=='用户'){
                            $('#users').append('<tr> <td>编号</td> <td>用户名</td> <td>用户头像</td> <td>操作</td> </tr>');
                            for (var i=0;i<10;i++) {
                                $.ajax({
                                    url:'/hui/TestController/getPoints.do?userid='+datas.result[i]['id'],
                                    async:false,
                                    headers:{
                                        accessToken:localStorage.getItem('adminAccessToken')
                                    },
                                    success:function (datap) {
                                        checkToken(datap);
                                        var n=parseInt(i)+1;
                                        if (datas.result[i]['ban']!=1)
                                            $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td><td><img class="img img-circle" src="/'+datas.result[i]['pic']+'" alt="" style="width: 50px;height: 50px"></td><td>'+datap.result.point+'</td><td><a href="#" onclick="ban('+datas.result[i]['id']+')">封禁</a>|<a href="#" onclick="deleteUserById('+datas.result[i]['id']+')">删除</a></td></tr>');
                                    }
                                })}
                        }
                        if (searchType=='封禁用户'){
                            $('#users').append('<tr> <td>编号</td> <td>用户名</td> <td>用户头像</td> <td>操作</td> </tr>');
                            for (var i=0;i<10;i++) {
                                $.ajax({
                                    url:'/hui/TestController/getPoints.do?userid='+datas.result[i]['id'],
                                    async:false,
                                    headers:{
                                        accessToken:localStorage.getItem('adminAccessToken')
                                    },
                                    success:function (datap) {
                                        checkToken(datap);
                                        var n=parseInt(i)+1;
                                        $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td><td><img class="img img-circle" src="/'+datas.result[i]['pic']+'" alt="" style="width: 50px;height: 50px"></td><td>'+datap.result.point+'</td><td><a href="#" onclick="unban('+datas.result[i]['id']+')">解封</a>|<a href="#" onclick="deleteUserById('+datas.result[i]['id']+')">删除</a></td></tr>');
                                    }
                                })
                            }
                        }
                        if (searchType=='待审核图片'){
                            $('#users').append('<tr> <td>编号</td> <td>类型</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>上传人</td> <td>操作</td> </tr>');
                            for (var i=0;i<10;i++) {
                                var n=parseInt(i)+1;
                                console.log('auditimage:'+datas.result);
                                $.ajax({
                                    url: "/hui/TestController/getUserById.do?userid=" + datas.result[i]['userid'],
                                    type: "get",
                                    async: false,
                                    success: function (data) {
                                        $.ajax({
                                            url: "/admin/getTypeById.do?typeid=" + datas.result[i]['typeid'],
                                            type: "get",
                                            async: false,
                                            headers:{
                                                accessToken:localStorage.getItem('adminAccessToken')
                                            },
                                            success: function (datat) {
                                                checkToken(datat);
                                                $('#users').append('<tr><td>' + n + '</td><td>'+datat.result.name+'</td><td>' + datas.result[i]['title'] + '</td><td>' + datas.result[i]['intro'] + '</td><td><img class="img img-rounded" src="/' + datas.result[i]['imgname'] + '" alt="" style="width: 50px;height: 50px"></td><td>' + data.result['name'] + '</td><td><a href="#" onclick="auditImg('+datas.result[i]['id']+')">通过|</a><a href="#" onclick="unauditImg(' + datas.result[i]['id'] + ')">不通过</a></td></tr>');
                                            }
                                        })
                                    }
                                })
                            }
                        }
                        if (searchType=='评论'){
                                    $('#users').empty();
                                    $('#page').show();
                                    $('#users').append('<tbody id="rightTable">');

                                    $('#rightTable').append('<tr> <td>编号</td> <td>用户</td> <td>评论</td>  <td>操作</td> </tr>');
                                    for (var i=0;i<10;i++) {
                                        var n = parseInt(i) + 1;
                                        console.log('评论id:' + datas.result[i]);
                                        $('#rightTable').append('<tr><td>'+n+'</td><td><a href="#" onclick="gotoHimself(' + datas.result[i]['userid'] + ')"><img src="/' + datas.result[i]['pic'] + '" class="img-circle" style="width: 30px;height: 30px">' + datas.result[i]['username'] + '</a></td> ' +
                                            '<td>' + datas.result[i]['content'] + '</td><td><a href="#" onclick="delComment('+datas.result[i]['id']+')">删除</a></td></tr>');
                                    }
                        }
                    $('#users').append('</tbody>');

                }
            })
    }
    function update() {
        var pic=document.getElementById('pic').files[0];
        var name=document.getElementById('user').value;
        var formData = new FormData();
        if (pic!=undefined)
            formData.append('pic', pic);
        if (name!='null')
            formData.append('name', name);
        formData.append('adminid',adminid);
        $.ajax({
            url: "/admin/update.do",
            type: "post",
            data: formData,
            contentType: false,
            processData: false,
            mimeType: "multipart/form-data",
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (data) {
                checkToken(data);
                console.log(data);
                data=JSON.parse(data);
                console.log(data.result.name);
                localStorage.setItem('adminAccessToken',data.accessToken);
                sessionStorage.setItem('adminPic',data.result.pic);
                sessionStorage.setItem('adminName',data.result.name);
                alert('修改成功');
                window.location.reload();
                console.log(data);
            },
            error: function (data) {
                console.log(data);
            }
        });

    }

    function getUsers() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='user';
        $.ajax({
            url: "/admin/getUsers.do",
            type: "get",
            contentType: false,
            processData: false,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (datas) {
                checkToken(datas);
                skipPages(datas.result.length);

                $('#users').empty();
                $('#page').show();
                $('#users').append('<tbody>');
                $('#users').append('<tr> <td>编号</td> <td>用户名</td> <td>用户头像</td> <td>积分</td> <td>操作</td> </tr>');
                for (var i=0;i<10;i++) {
                    $.ajax({
                        url:'/hui/TestController/getPoints.do?userid='+datas.result[i]['id'],
                        async:false,
                        headers:{
                            accessToken:localStorage.getItem('adminAccessToken')
                        },
                        success:function (datap) {
                            checkToken(datap);
                            var n=parseInt(i)+1;
                            if (datas.result[i]['ban']!=1)
                                $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td><td><img class="img img-circle" src="/'+datas.result[i]['pic']+'" alt="" style="width: 50px;height: 50px"></td><td>'+datap.result.point+'</td><td><a href="#" onclick="ban('+datas.result[i]['id']+')">封禁</a>|<a href="#" onclick="deleteUserById('+datas.result[i]['id']+')">删除</a></td></tr>');
                        }
                    })}
                $('#users').append('</tbody>');

            },
            error: function (datas) {
                console.log(datas);
            }
        });
    }
    function getBanUser() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='banuser';
        $.ajax({
            url: "/admin/getBanUsers.do",
            type: "get",
            contentType: false,
            processData: false,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (datas) {
                checkToken(datas);
                skipPages(datas.result.length);

                $('#users').empty();
                $('#page').show();
                $('#users').append('<tbody>');
                $('#users').append('<tr> <td>编号</td> <td>用户名</td> <td>用户头像</td> <td>积分</td> <td>操作</td> </tr>');
                for (var i=0;i<10;i++) {
                    $.ajax({
                        url:'/hui/TestController/getPoints.do?userid='+datas.result[i]['id'],
                        async:false,
                        headers:{
                            accessToken:localStorage.getItem('adminAccessToken')
                        },
                        success:function (datap) {
                            checkToken(datap);
                            var n=parseInt(i)+1;
                            $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td><td><img class="img img-circle" src="/'+datas.result[i]['pic']+'" alt="" style="width: 50px;height: 50px"></td><td>'+datap.result.point+'</td><td><a href="#" onclick="unban('+datas.result[i]['id']+')">解封</a>|<a href="#" onclick="deleteUserById('+datas.result[i]['id']+')">删除</a></td></tr>');
                        }
                    })
                    }
                $('#users').append('</tbody>');
            },
            error: function (datas) {
                console.log(datas);
            }
        });
    }

    function deleteUserById(userid) {
        if (!confirm("确定删除?")){
            return;
        }
        $.ajax({
            url: "/admin/deleteUserById.do?userid="+userid,
            type:"get",
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (data) {
                checkToken(data);
                console.log(data);
                if (data.resMsg!='删除失败') {
                alert("删除成功");
                getUsers();
                }
            },
            error:function (data) {
                console.log(data);
                alert("删除失败");

            }
        })
    }
    function ban(userid) {
        $.ajax({
            url:"/admin/banUser.do?userid="+userid,
            type:"get",
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                console.log(datas);
                if (datas.resMsg!='封禁失败') {
                    alert('封禁成功');
                    getUsers();
                }else {
                    alert('封禁失败');
                }
            },
            error:function (datas) {
                console.log(datas);
                alert('封禁失败');
            }
        })
    }
    function unban(userid) {
        $.ajax({
            url: "/admin/unbanUser.do?userid="+userid,
            type:"get",
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                console.log(datas);
                if (datas.resMsg!='解封失败') {
                    alert('解封成功');
                    getBanUser();
                }else {
                    alert('解封失败');
                }
            },
            error:function (datas) {
                console.log(datas);
                alert("解封失败");
            }
        })
    }

    function getAuditImgs() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='audit';
        $.ajax({
                url:'/admin/getAuditImgs.do',
                type:'get',
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
                success:function (datas) {
                    checkToken(datas);
                    skipPages(datas.result.length);

                    $('#users').empty();
                    $('#page').show();
                    $('#users').append('<tbody>');
                    $('#users').append('<tr> <td>编号</td> <td>类型</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>上传人</td> <td>操作</td> </tr>');
                    for (var i=0;i<10;i++) {
                        var n=parseInt(i)+1;
                        console.log('auditimage:'+datas.result);
                        $.ajax({
                            url: "/hui/TestController/getUserById.do?userid=" + datas.result[i]['userid'],
                            type: "get",
                            async: false,
                            success: function (data) {
                                $.ajax({
                                    url: "/admin/getTypeById.do?typeid=" + datas.result[i]['typeid'],
                                    type: "get",
                                    async: false,
                                    success: function (datat) {
                                        $('#users').append('<tr><td>' + n + '</td><td>'+datat.result.name+'</td><td>' + datas.result[i]['title'] + '</td><td>' + datas.result[i]['intro'] + '</td><td><img class="img img-rounded" src="/' + datas.result[i]['imgname'] + '" alt="" style="width: 50px;height: 50px"></td><td>' + data.result['name'] + '</td><td><a href="#" onclick="auditImg('+datas.result[i]['id']+')">通过|</a><a href="#" onclick="unauditImg(' + datas.result[i]['id'] + ')">不通过</a></td></tr>');
                                    }
                                })
                            }
                        })
                    }
                    $('#users').append('</tbody>');

                }
            })
    }

    function auditImg(imgid) {
        $.ajax({
            url:'/admin/auditImg.do?imgid='+imgid,
            type:'get',
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                if (datas.resMsg!='审核失败')
                    alert('操作成功');
                else
                    alert('操作失败');
                getAuditImgs();
            }
        })

    }

    function unauditImg(imgid) {
        $.ajax({
            url:'/admin/unauditImg.do?imgid='+imgid,
            type:'get',
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                if (datas.resMsg!='审核失败')
                    alert('操作成功');
                else
                    alert('操作失败');
                getAuditImgs();
            }
        })

    }
    function getTypes() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='type';
        $.ajax({
            url: "/admin/getTypes.do",
            type: "get",
            contentType: false,
            processData: false,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (datas) {
                checkToken(datas);
                skipPages(datas.result.length);

                $('#users').empty();
                $('#page').show();
                $('#users').append('<tbody>');

                $('#users').append('<tr> <td>编号</td> <td>类型名</td><td>操作</td> </tr>');
                for (var i=0;i<10;i++) {
                    var n=parseInt(i)+1;
                    $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td></td><td><a href="#" onclick="updateTypeView('+datas.result[i]['id']+')">修改</a>|<a href="#" onclick="deleteTypeById('+datas.result[i]['id']+')">删除</a></td></tr>');
                }
                $('#users').append('</tbody>');
            },
            error: function (datas) {
                console.log(datas);
            }
        });
    }

    function updateTypeView(typeid) {
        $('#users').empty();
        $('#page').hide();
        $.ajax({
            url:'/admin/getTypeById.do?typeid='+typeid,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                $('#users').append('<div class="form-horizontal">  <div class="form-group"> <label for="pic" class="col-sm-2 control-label">类型名</label> <div class="col-sm-10"> <input type="text" style="width: 25%;" class="form-control" value="'+datas.result.name+'" id="type" name="type"> </div> </div> <div class="form-group" style="text-align: left"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" onclick="updateType('+typeid+')">修改</button> </div> </div> </div>');

            }
        })
    }

    function updateType(typeid) {
        var name=document.getElementById('type').value;
        $.ajax({
            url:'/admin/updateType.do?typeid='+typeid+'&name='+name,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                if (datas.resMsg!='修改失败') {
                    alert('修改成功');
                }else
                    alert('修改失败');
                skip('type',sessionStorage.getItem('page2'));
            }
        })
    }

    function deleteTypeById(typeid) {
        if (!confirm('确认删除?')){
            return;
        }
        $.ajax({
            url:'/admin/deleteType.do?typeid='+typeid,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                if (datas.resMsg!='删除失败'&&datas.resMsg!='删除失败，该类型还有图片')
                    alert('删除成功');
                else if (datas.resMsg=='删除失败，该类型还有图片')
                    alert('删除失败，该类型还有图片');
                else
                    alert('删除失败');
                skip('type',sessionStorage.getItem('page2'));
            }
        })
    }

    function getImgs() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='img';
        $.ajax({
            url: "/hui/TestController/getImgs.do",
            type: "get",
            contentType: false,
            processData: false,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (datas) {
                checkToken(datas);
                skipPages(datas.result.length);

                $('#users').empty();
                $('#page').show();
                $('#users').append('<tbody>');

                $('#users').append('<tr> <td>编号</td> <td>类型</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>上传人</td> <td>点赞数</td> <td>操作</td> </tr>');
                for (var i=0;i<10;i++) {
                    var n=parseInt(i)+1;
                        $.ajax({
                            url: "/hui/TestController/getUserById.do?userid=" + datas.result[i]['userid'],
                            type: "get",
                            async: false,
                            success: function (data) {
                                $.ajax({
                                    url: "/admin/getTypeById.do?typeid=" + datas.result[i]['typeid'],
                                    type: "get",
                                    async: false,
                                    headers:{
                                        accessToken:localStorage.getItem('adminAccessToken')
                                    },
                                    success: function (datat) {
                                        checkToken(datat);
                                        $('#users').append('<tr><td>' + n + '</td><td>'+datat.result.name+'</td><td>' + datas.result[i]['title'] + '</td><td>' + datas.result[i]['intro'] + '</td><td><img class="img img-rounded" src="/' + datas.result[i]['imgname'] + '" alt="" style="width: 50px;height: 50px"></td><td>' + data.result['name'] + '</td><td>' + datas.result[i]['thumb'] + '</td><td><a href="#" onclick="deleteImgById(' + datas.result[i]['id'] + ')">删除</a></td></tr>');

                                    }
                                })
                            }
                        })
                    }
                $('#users').append('</tbody>');

            },
            error: function (datas) {
                console.log(datas);
            }
        });
    }

    function getComments() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='comment';
        $.ajax({
            url: "/admin/getComments.do",
            type: "get",
            contentType: false,
            processData: false,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (comments) {
                checkToken(comments);
                skipPages(comments.result.length);

                $('#users').empty();
                $('#page').show();
                $('#users').append('<tbody id="rightTable">');

                $('#rightTable').append('<tr> <td>编号</td> <td>用户</td> <td>评论</td>  <td>操作</td> </tr>');
                for (var i=0;i<10;i++) {
                    var n = parseInt(i) + 1;
                    console.log('评论id:' + comments.result[i]);
                    $('#rightTable').append('<tr><td>'+n+'</td><td><a href="#" onclick="gotoHimself(' + comments.result[i]['userid'] + ')"><img src="/' + comments.result[i]['pic'] + '" class="img-circle" style="width: 30px;height: 30px">' + comments.result[i]['username'] + '</a></td> ' +
                        '<td>' + comments.result[i]['content'] + '</td><td><a href="#" onclick="delComment('+comments.result[i]['id']+')">删除</a></td></tr>');
                }
            },
            error: function (datas) {
                console.log(datas);
            }
        });
    }
    
    function deleteImgById(imgid) {
        if (!confirm('确认删除?')) {
            return;
        }
        $.ajax({
            url:'/admin/deleteImgById.do?imgid='+imgid,
            type:'get',
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                if (datas.resMsg!='删除失败')
                    alert('删除成功');
                else
                    alert('删除失败');
                getImgs();
            },error:function (datas) {
                alert('删除失败2');
            }
        })
    }

    function skip(type,flag) {
        if (flag==-1)
            var page2=parseInt(sessionStorage.getItem('page2'))-1;
        else if (flag==0)
            var page2=parseInt(sessionStorage.getItem('page2'))+1;
        else
            var page2=flag;
        if (page2<=0)
            page2=1;
        if(page2>pages)
            page2=pages;
        console.log('sss='+page2);
        sessionStorage.setItem('page2',page2);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        $('#toPageNum').val(page2);
        $('#users').empty();
        $('#users').append('<tbody>');
        if (type=='search'){
            mySearch(page2);
        }
        if (type=='img'){
        $.ajax({
            url: "/hui/TestController/getImgs.do",
            type: "get",
            contentType: false,
            processData: false,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (datas) {
                checkToken(datas);
                console.log(datas);
                $('#page').show();
                $('#users').append('<tr> <td>编号</td> <td>类型</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>上传人</td> <td>点赞数</td> <td>操作</td> </tr>');
                for (var i=(page2-1)*10;i<page2*10;i++) {
                    var n=parseInt(i)+1;
                    $.ajax({
                        url: "/hui/TestController/getUserById.do?userid=" + datas.result[i]['userid'],
                        type: "get",
                        async: false,
                        success: function (data) {
                            $.ajax({
                                url: "/admin/getTypeById.do?typeid=" + datas.result[i]['typeid'],
                                type: "get",
                                async: false,
                                headers:{
                                    accessToken:localStorage.getItem('adminAccessToken')
                                },
                                success: function (datat) {
                                    checkToken(datat);
                                    $('#users').append('<tr><td>' + n + '</td><td>'+datat.result.name+'</td><td>' + datas.result[i]['title'] + '</td><td>' + datas.result[i]['intro'] + '</td><td><img class="img img-rounded" src="/' + datas.result[i]['imgname'] + '" alt="" style="width: 50px;height: 50px"></td><td>' + data.result['name'] + '</td><td>' + datas.result[i]['thumb'] + '</td><td><a href="#" onclick="deleteImgById(' + datas.result[i]['id'] + ')">删除</a></td></tr>');
                                }
                            })
                        }
                    })
                }
            },
            error: function (datas) {
                console.log(datas);
            }
        });
        }
        if (type=='user'){
            $.ajax({
                url: "/admin/getUsers.do",
                type: "get",
                contentType: false,
                processData: false,
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success: function (datas) {
                    checkToken(datas);
                    $('#page').show();
                    $('#users').append('<tr> <td>编号</td> <td>用户名</td> <td>用户头像</td> <td>积分</td> <td>操作</td> </tr>');
                    for (var i=(page2-1)*10;i<page2*10;i++) {
                        var n=parseInt(i)+1;
                        if (datas.result[i]['ban']!=1)
                            $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td><td><img class="img img-circle" src="/'+datas.result[i]['pic']+'" alt="" style="width: 50px;height: 50px"></td><td>'+datas.result[i]['point']+'</td><td><a href="#" onclick="ban('+datas.result[i]['id']+')">封禁</a>|<a href="#" onclick="deleteUserById('+datas.result[i]['id']+')">删除</a></td></tr>');
                    }
                },
                error: function (datas) {
                    console.log(datas);
                }
            });
        }
        if (type=='banuser'){
            $.ajax({
                url: "/admin/getBanUsers.do",
                type: "get",
                contentType: false,
                processData: false,
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success: function (datas) {
                    checkToken(datas);
                    $('#page').show();
                    $('#users').append('<tr> <td>编号</td> <td>用户名</td> <td>用户头像</td> <td>积分</td> <td>操作</td> </tr>');
                    for (var i=(page2-1)*10;i<page2*10;i++) {
                        var n=parseInt(i)+1;
                        $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td><td><img class="img img-circle" src="/'+datas.result[i]['pic']+'" alt="" style="width: 50px;height: 50px"></td><td>'+datas.result[i]['point']+'</td><td><a href="#" onclick="unban('+datas.result[i]['id']+')">解封</a>|<a href="#" onclick="deleteUserById('+datas.result[i]['id']+')">删除</a></td></tr>');
                    }
                },
                error: function (datas) {
                    console.log(datas);
                }
            });
        }
        if (type=='type'){
            $.ajax({
                url: "/admin/getTypes.do",
                type: "get",
                contentType: false,
                processData: false,
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success: function (datas) {
                    checkToken(datas);
                    $('#page').show();
                    $('#users').append('<tr> <td>编号</td> <td>类型名</td><td>操作</td> </tr>');
                    for (var i=(page2-1)*10;i<page2*10;i++) {
                        var n=parseInt(i)+1;
                        $('#users').append('<tr><td>'+n+'</td><td>'+datas.result[i]['name']+'</td></td><td><a href="#" onclick="updateTypeView('+datas.result[i]['id']+')">修改</a>|<a href="#" onclick="deleteTypeById('+datas.result[i]['id']+')">删除</a></td></tr>');
                    }
                },
                error: function (datas) {
                    console.log(datas);
                }
            });
        }
        if (type=='audit'){
            $.ajax({
                url:'/admin/getAuditImgs.do',
                type:'get',
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success:function (datas) {
                    checkToken(datas);
                    $('#page').show();
                    $('#users').append('<tr> <td>编号</td> <td>类型</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>上传人</td> <td>操作</td> </tr>');
                    for (var i=(page2-1)*10;i<page2*10;i++) {
                        var n=parseInt(i)+1;
                        console.log('auditimage:'+datas.result);
                        $.ajax({
                            url: "/hui/TestController/getUserById.do?userid=" + datas.result[i]['userid'],
                            type: "get",
                            async: false,
                            success: function (data) {
                                $.ajax({
                                    url: "/admin/getTypeById.do?typeid=" + datas.result[i]['typeid'],
                                    type: "get",
                                    async: false,
                                    headers:{
                                        accessToken:localStorage.getItem('adminAccessToken')
                                    },
                                    success: function (datat) {
                                        checkToken(datat);
                                        $('#users').append('<tr><td>' + n + '</td><td>'+datat.result.name+'</td><td>' + datas.result[i]['title'] + '</td><td>' + datas.result[i]['intro'] + '</td><td><img class="img img-rounded" src="/' + datas.result[i]['imgname'] + '" alt="" style="width: 50px;height: 50px"></td><td>' + data.result['name'] + '</td><td><a href="#" onclick="auditImg('+datas.result[i]['id']+')">通过|</a><a href="#" onclick="unauditImg(' + datas.result[i]['id'] + ')">不通过</a></td></tr>');
                                    }
                                })
                            }
                        })
                    }
                }
            })
        }
        if (type=='deposit'){
            $('#users').append('<tb ody>')
            $('#users').append('<tr> <td>编号</td> <td>积分</td> <td>用户</td> <td>操作</td> </tr>');
            $.ajax({
                url:'/admin/getDeposits.do',
                success:function (datas) {
                    for (var i=(page2-1)*10;i<page2*10;i++){
                        $.ajax({
                            url:'/hui/TestController/getUserById.do?userid='+datas.result[i]['uid'],
                            async:false,
                            success:function (data) {
                                $('#users').append('<tr> <td>'+i+1+'</td> <td>'+datas.result[i]['point']+'</td> <td>'+data.result.name+'</td> <td><a href="#" onclick="addPoint('+datas.result[i]['point']+','+datas.result[i]['id']+','+datas.result[i]['uid']+')">通过|</a><a href="#" onclick="addPoint(0)">不通过</a></td> </tr>')
                            }
                        })
                    }
                }
            })
        }
        if(type=="comment"){
            $.ajax({
                url: "/admin/getComments.do",
                type: "get",
                contentType: false,
                processData: false,
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success: function (comments) {
                    checkToken(comments);
                    $('#users').empty();
                    $('#page').show();
                    $('#users').append('<tbody id="rightTable">');

                    $('#rightTable').append('<tr> <td>编号</td> <td>用户</td> <td>评论</td>  <td>操作</td> </tr>');
                    for (var i=(page2-1)*10;i<page2*10;i++) {
                        var n = parseInt(i) + 1;
                        console.log('评论id:' + comments.result[i]);
                        $('#rightTable').append('<tr><td>'+n+'</td><td><a href="#" onclick="gotoHimself(' + comments.result[i]['userid'] + ')"><img src="/' + comments.result[i]['pic'] + '" class="img-circle" style="width: 30px;height: 30px">' + comments.result[i]['username'] + '</a></td> ' +
                            '<td>' + comments.result[i]['content'] + '</td><td><a href="#" onclick="delComment('+comments.result[i]['id']+')">删除</a></td></tr>');
                    }
                },
                error: function (datas) {
                    console.log(datas);
                }
            });
        }
        if (type=='complain'){
            $('#users').empty();
            $('#users').append('<tbody>')
            $('#users').append('<tr> <td>编号</td> <td>评论内容</td> <td>评论人</td><td>举报人</td> <td>操作</td> </tr>');
            $.ajax({
                url: '/admin/getComplains.do',
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success: function (datas) {
                    checkToken(datas);
                    for (var i = 0; i < datas.result.length; i++){
                        var n=i+1;
                        $('#users').append('<tr> <td>' + n + '</td> <td>' + datas.result[i]['content'] + '</td> <td><img src="/'+datas.result[i]['comupic']+'" class="img-circle" style="width: 30px;height: 30px">' + datas.result[i]['comuname'] + '</td><td><img src="/'+datas.result[i]['upic']+'" class="img-circle" style="width: 30px;height: 30px">' + datas.result[i]['uname'] + '</td> <td><a href="#" onclick="handle('+datas.result[i]['id']+','+datas.result[i]['comid']+')">处理|</a><a href="#" onclick="unhandle('+datas.result[i]['id']+')">不处理</a></td> </tr>')
                    }
                }
            })
        }
        $('#users').append('</tbody>');


    }

    function delComment(comid) {
        if (!confirm('确认删除?'))
            return;
        $.ajax({
            url:'/hui/TestController/delComments.do?comid='+comid,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (data) {
                checkToken(data);
                if (data.resMsg!='删除失败'){
                    getComments();
                }else{
                    alert('删除失败');
                }
            }
        })
    }

    function logout() {
        if(!confirm('确认注销?'))
            return;
        sessionStorage.removeItem('adminid');
        localStorage.removeItem('adminAccessToken');
        window.location.href='index.html';
    }

    function getDeposit() {

        sessionStorage.setItem('page2',1);
        document.getElementById('curpage').innerText=sessionStorage.getItem('page2');
        type='deposit';
        $('#users').empty();
        $('#users').append('<tbody>')
        $('#users').append('<tr> <td>编号</td> <td>积分</td> <td>用户</td> <td>操作</td> </tr>');
        $.ajax({
            url:'/admin/getDeposits.do',
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                skipPages(datas.result.length);

                for (var i=0;i<10;i++){
                    $.ajax({
                        url:'/hui/TestController/getUserById.do?userid='+datas.result[i]['uid'],
                        async:false,
                        success:function (data) {
                            $('#users').append('<tr> <td>'+i+'</td> <td>'+datas.result[i]['point']+'</td> <td>'+data.result.name+'</td> <td><a href="#" onclick="addPoint('+datas.result[i]['point']+','+datas.result[i]['id']+','+datas.result[i]['uid']+')">通过|</a><a href="#" onclick="addPoint(0,'+datas.result[i]['id']+','+datas.result[i]['uid']+')">不通过</a></td> </tr>')
                        }
                    })
                }
            }
        })
    }
    function addPoint(p,id,uid) {
        $.ajax({
            url:'/admin/addPoint.do?id='+id+'&uid='+uid+'&point='+p,
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success:function (datas) {
                checkToken(datas);
                console.log(datas);
                if (datas.resMsg=='添加成功')
                    alert('操作成功');
                else
                    alert('操作失败');
                getDeposit();
            }
        })
    }

    function getComplains() {

        sessionStorage.setItem('page2', 1);
        document.getElementById('curpage').innerText = sessionStorage.getItem('page2');
        type='complain';
        $('#users').empty();
        $('#users').append('<tbody>')
        $('#users').append('<tr> <td>编号</td> <td>评论内容</td> <td>评论人</td><td>举报人</td> <td>操作</td> </tr>');
        $.ajax({
            url: '/admin/getComplains.do',
            headers:{
                accessToken:localStorage.getItem('adminAccessToken')
            },
            success: function (datas) {
                checkToken(datas);
                skipPages(datas.result.length);

                for (var i = 0; i < datas.result.length; i++){
                    var n=i+1;
                    $('#users').append('<tr> <td>' + n + '</td> <td>' + datas.result[i]['content'] + '</td> <td><img src="/'+datas.result[i]['comupic']+'" class="img-circle" style="width: 30px;height: 30px">' + datas.result[i]['comuname'] + '</td><td><img src="/'+datas.result[i]['upic']+'" class="img-circle" style="width: 30px;height: 30px">' + datas.result[i]['uname'] + '</td> <td><a href="#" onclick="handle('+datas.result[i]['id']+','+datas.result[i]['comid']+')">处理|</a><a href="#" onclick="unhandle('+datas.result[i]['id']+')">不处理</a></td> </tr>')
                }}
        })

    }
    function handle(compid,comid) {
        if (!confirm('确认处理?')){
            return;
        }
        delCom(comid);
        delComplain(compid);
    }

    function unhandle(compid) {
        if (!confirm('确认不处理?')){
            return;
        }
        delComplain(compid);
    }
        function delComplain(compid) {
            $.ajax({
                url:'/admin/delComplains.do?compid='+compid,
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success:function (data) {
                    checkToken(data);
                    if (data.resMsg!='删除失败'){
                        getComplains();
                    }else {
                        alert('删除失败');
                    }
                }
            })
        }

        function delCom(comid) {
            $.ajax({
                url:'/hui/TestController/delComments.do?comid='+comid,
                headers:{
                    accessToken:localStorage.getItem('adminAccessToken')
                },
                success:function (data) {
                    checkToken(data);
                    if (data.resMsg!='删除失败'){
                    }else {
                        alert('删除失败');
                    }
                }
            })
        }

    function skipPages(dataLength) {
        $('#skips').empty();
        pages=parseInt(dataLength/10);
        if (dataLength%10!=0)
            pages+=1;
        console.log('页数是:'+pages);
        var loop=pages;
        loop=loop>5?5:loop;
        console.log('循环次数是:'+pages);
        for (var i = 0; i < loop; i++) {
            if (i == 0) {
                $('#skips').append("                    <li><a id='skip" + i + "' href='#' onclick='skipReady(1)'>first</a></li>"+
                    "<li>\n" +
                    "                        <a href=\"#\" aria-label=\"Previous\" onclick='skipReady(-1)' id=\"skippre\">\n" +
                    "                            <span aria-hidden=\"true\">&laquo;</span>\n" +
                    "                        </a>\n" +
                    "                    </li>\n");
            }

            $('#skips').append(
                "                    <li><a id='skip" + i + "' href='#' onclick='skipReady(" + (i + 1) + ")'>" + (i + 1) + "</a></li>"
            );
            if (i == loop - 1) {
                if (pages > 5) {
                    $('#skips').append(
                        "<li><a>...</a></li>" +
                        "                    <li><a id='skip" + pages + "' href='#' onclick='skipReady(" + pages + ")'>" + pages + "</a></li>")
                }

                $('#skips').append(
                    "                    <li>\n" +
                    "                        <a id=\"skipnex\" href=\"#\" aria-label=\"Next\" onclick='skipReady(0)'>\n" +
                    "                            <span aria-hidden=\"true\">&raquo;</span>\n" +
                    "                        </a>\n" +
                    "                    </li>"+
                    "<li><a id='skip" + i + "' href='#' onclick='skipReady("+pages+")'>end</a></li>");
            }
        }
    }



    function skipReady(page){
        skip(type,page);
    }
</script>
</html>